package com.iiapk.gridnew;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;

import com.iiapk.grid.database.ArticleManager;
import com.iiapk.gridnew.domain.Article;
import com.iiapk.gridnew.fragment.NewsPanelFragment;

public class NewsPanelPagerActivity extends FragmentActivity {

	private FragmentPagerAdapter gridPagerAdapter;
	private ViewPager gridPager;
	private List<Fragment> pageviews= new ArrayList<Fragment>();
	LayoutAnimationController controller;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.panel);
		String columnId = this.getIntent().getStringExtra("id");
		//String title = this.getIntent().getStringExtra("title");
		/*TextView textView = (TextView)findViewById(R.id.panel_header_text);
		textView.setText(title);*/
		ArticleManager articleManager = new ArticleManager(this);
		List<Article> list = articleManager.select(columnId);
		
		list.get(0).imgR=R.drawable.test1;
		list.get(1).imgR=R.drawable.test2;
		list.get(2).imgR=R.drawable.test3;
		list.get(3).imgR=R.drawable.test4;
		list.get(4).imgR=R.drawable.test5;
		list.get(5).imgR=R.drawable.test6;
		list.get(6).imgR=R.drawable.test7;
		
		pageviews.add(new NewsPanelFragment(list));
		pageviews.add(new NewsPanelFragment(list));
		pageviews.add(new NewsPanelFragment(list));
		pageviews.add(new NewsPanelFragment(list));
		pageviews.add(new NewsPanelFragment(list));
		
		gridPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager());
		
		gridPager = (ViewPager) findViewById(R.id.main_gridpager);
		controller = AnimationUtils.loadLayoutAnimation(this,R.anim.layout_random_fade_im);
		gridPager.setLayoutAnimation(controller);
		gridPager.setAdapter(gridPagerAdapter);
	}

	@Override
	protected void onResume() {
		super.onResume();
		gridPager.startLayoutAnimation();
	}

	private class FragmentPagerAdapter extends FragmentStatePagerAdapter {
		public FragmentPagerAdapter(FragmentManager fm) {
			super(fm);
		}
		@Override
		public int getCount() {
			return pageviews.size();
		}
		@Override
		public Fragment getItem(int position) {
			return pageviews.get(position);
		}
		
		@Override
		public Object instantiateItem(View arg0, int arg1) {
			Fragment view = (Fragment)super.instantiateItem(arg0, arg1);
	        return view;
		}
	
	}
}